<%-- 
    Document   : administracaoLogAcesso
    Created on : 26/10/2010, 22:54:47
    Author     : Thiago
--%>

<%@page import="ferramentas.DataHora"%>
<%@page import="dao.LogAcessoDAO"%>
<%@page import="dao.ModuloDAO"%>
<%@page import="crypt.Crypt"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="bo.*"%>
<%@page import="beans.*"%>
<%@page import="autenticacao.Sessao"%>
<%

            String moduleId = "26";
            Crypt crypt = new Crypt();
            try {
                crypt.setKey(crypt.Decifragem((String) session.getAttribute("userPhrase")));
                String desafio = crypt.Cifragem(moduleId + "@" + ferramentas.DataHora.formatarDataHora(ferramentas.DataHora.retornarDataHoraAtual(), ferramentas.DataHora.DATE_TIME_FORMAT));
                if (!(new Sessao(session).validarSessao(desafio))) {
                    response.sendRedirect("/" + request.getContextPath());
                }
            } catch (Exception e) {
                response.sendRedirect("/" + request.getContextPath());
            }

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Gestor Operacional</title>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/style.css" type="text/css" rel="stylesheet"/>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/table.css" type="text/css" rel="stylesheet"/>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/anytime.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/jquery.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/anytime.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/anytimetz.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/table.js"></script>
    </head>
    <body>
        <table width="100%">
            <tr>
                <td>
                    <jsp:include page = "../menu.jsp" flush = "true"/>
                </td>
            </tr>

            <tr align="center">
                <td><br/><br/>
                    <form name="form1" action="administracaoLogAcesso.jsp" method="post">
                        <input type="hidden" name="exibirRelatorio" value="true">
                        <table border="1" cellspacing="0" width="450" bgcolor="#FFFFFF">
                            <tr>
                                <td width="110">Usuario:</td>
                                <td align="center">
                                    <select name="usuario" size="1">
                                        <option value="" selected>Todos</option>
                                        <%
                                                    ArrayList<Usuario> alu = null;
                                                    try {
                                                        alu = new UsuarioBO().recuperarUsuarios(null);
                                                    } catch (Exception e) {
                                                        out.println(e.getMessage());
                                                    }
                                                    if (alu != null) {
                                                        for (int i = 0; i < alu.size(); i++) {
                                                            out.println("<option value=\"" + alu.get(i).getId() + "\">" + alu.get(i).getId() + " - " + alu.get(i).getDescricao() + "</option>");
                                                        }
                                                    }

                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>Tipo Log:</td>
                                <td align="center">
                                    <select name="tipo" size ="1">
                                        <option value="" selected>Todos</option>
                                        <option value="0">Login com sucesso</option>
                                        <option value="1">Acesso a Módulo com sucesso</option>
                                        <option value="2">Tentativa de login inválido</option>
                                        <option value="3">Tentativa de acesso a módulo inválida</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>Modulo:</td>
                                <td align="center">
                                    <select name="processo" size ="1">
                                        <option value="" selected>Todos</option>
                                        <%
                                                    ArrayList<Modulo> alm = null;
                                                    try {
                                                        alm = new ModuloDAO().recuperarModulos(null);
                                                    } catch (Exception e) {
                                                        out.println(e.getMessage());
                                                    }
                                                    if (alm != null) {
                                                        for (int i = 0; i < alm.size(); i++) {
                                                            out.println("<option value=\"" + alm.get(i).getId() + "\">" + alm.get(i).getDescricao() + "</option>");
                                                        }
                                                    }

                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>Data Log Inicio:</td>
                                <td align="center">
                                    <input id="data" name="data" type="text" maxlength="10" size="10">
                                    <script type="text/javascript">
                                        AnyTime.picker( "data",
                                        { format: "%d/%m/%z", firstDOW: 0 } );
                                    </script>
                                </td>
                            </tr>
                            <tr>
                                <td>Data Log Fim:</td>
                                <td align="center">
                                    <input id="dataFim" name="dataFim" type="text" maxlength="10" size="10">
                                    <script type="text/javascript">
                                        AnyTime.picker( "dataFim",
                                        { format: "%d/%m/%z", firstDOW: 0 } );
                                    </script>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" align="center">
                                    <input type="submit">
                                </td>
                            </tr>
                        </table>
                    </form>
                </td>
            </tr>
            <tr>
                <td align="center"><br/><br/><hr>
                    <%
                                if ((request.getParameter("exibirRelatorio") != null) && request.getParameter("exibirRelatorio").equals("true")) {
                                    LogAcesso la = new LogAcesso();
                                    if ((request.getParameter("usuario") != null) && (!request.getParameter("usuario").isEmpty())) {
                                        la.setUsuario(new Usuario());
                                        la.getUsuario().setId(request.getParameter("usuario"));
                                    }
                                    if ((request.getParameter("modulo") != null) && (request.getParameter("modulo").length() > 0)) {
                                        la.setModulo(new Modulo());
                                        try {
                                            la.getModulo().setId(Integer.parseInt(request.getParameter("modulo")));
                                        } catch (Exception e) {
                                            System.out.println(e.getMessage());
                                        }
                                    }
                                    if ((request.getParameter("data") != null) && (request.getParameter("data").length() > 0)) {
                                        la.setDtLog(ferramentas.DataHora.retornarDataHora(request.getParameter("data") + " 00:00:00", "dd/MM/yyyy HH:mm:ss"));
                                    }
                                    if ((request.getParameter("dataFim") != null) && (request.getParameter("dataFim").length() > 0)) {
                                        la.setDtLogSuperior(ferramentas.DataHora.retornarDataHora(request.getParameter("dataFim") + " 23:59:59", "dd/MM/yyyy HH:mm:ss"));
                                    }
                                    if ((request.getParameter("tipo") != null) && (request.getParameter("tipo").length() > 0)) {
                                        la.setTipoLog(Integer.parseInt(request.getParameter("tipo")));
                                    }
                                    ArrayList<LogAcesso> all = null;
                                    all = new LogAcessoDAO().recuperarLogAcesso(la);
                                    if ((all != null) && (all.isEmpty())) {
                                        out.println("<table><tr><td align=\"center\">Nenhum resultado.</td></tr></table>");
                                    } else {
                    %>
                    <br/>
                    <br/>
                    <div class="widget_tableDiv">
                        <table id="myTable">
                            <thead align="center">
                                <tr>
                                    <td align="center"><b>Data do Log</b></td>
                                    <td align="center"><b>Código de Usuário</b></td>
                                    <td align="center"><b>Nome do Usuário</b></td>
                                    <td align="center"><b>Tipo do Log</b></td>
                                    <td align="center"><b>Módulo</b></td>
                                    <td align="center"><b>Observação</b></td>
                                </tr>
                            </thead>
                            <tbody class="scrollingContent">
                                <%
                                                                        for (int i = 0; i < all.size(); i++) {
                                                                            out.println("<tr>");
                                                                            out.println("<td align=\"center\">" + DataHora.formatarDataHora(all.get(i).getDtLog(), DataHora.DATE_TIME_FORMAT) + "</td>");
                                                                            out.println("<td align=\"center\">" + (all.get(i).getUsuario() != null ? all.get(i).getUsuario().getId() : "") + "</td>");
                                                                            out.println("<td align=\"center\">" + (all.get(i).getUsuario() != null ? all.get(i).getUsuario().getDescricao() : "") + "</td>");
                                                                            String tipo = "";
                                                                            switch (all.get(i).getTipoLog()) {
                                                                                case 0:
                                                                                    tipo = "Login com sucesso";
                                                                                    break;
                                                                                case 1:
                                                                                    tipo = "Acesso a Módulo com sucesso";
                                                                                    break;
                                                                                case 2:
                                                                                    tipo = "Tentativa de login inválido";
                                                                                    break;
                                                                                case 3:
                                                                                    tipo = "Tentativa de acesso a módulo inválida";
                                                                                    break;
                                                                                default:
                                                                                    tipo = "Inválido";
                                                                            }
                                                                            out.println("<td align=\"center\">" + tipo + "</td>");
                                                                            String modulo = "";
                                                                            if (all.get(i).getModulo() != null) {
                                                                                modulo = all.get(i).getModulo().getDescricao();
                                                                            }
                                                                            out.println("<td align=\"center\">" + modulo + "</td>");
                                                                            out.println("<td align=\"center\">" + all.get(i).getObservacao() + "</td>");
                                                                            out.println("</tr>");
                                                                        }
                                %>
                            </tbody>
                        </table>
                    </div>
                    <br><br>
                    <script type="text/javascript">
                        initTableWidget('myTable',900,350,Array('S','S','S','S','S','S'));
                    </script>
                    <%
                                    }
                                }
                    %>
                </td>
            </tr>
        </table>
    </body>
</html>